package com.zy.scwwebuis.controller;

import com.zy.scwcommon.bean.AppResponse;
import com.zy.scwwebuis.ScwWebuisApplication;
import com.zy.scwwebuis.service.TMemberServiceFeign;
import com.zy.scwwebuis.service.TProjectInfoServiceFeign;
import com.zy.scwwebuis.vo.resp.UserRespVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
public class DispatcherController {
    @Autowired
    TMemberServiceFeign memberServiceFeign;
    @Autowired
    TProjectInfoServiceFeign tProjectInfoServiceFeign;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * @Description: http://localhost:10000/index
     * @Param:  请求域 modle
     * @return:
     * @Author: zy123
     * @Date:
     */
    @RequestMapping("/index")
    public String index(Model modle, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        modle.addAttribute("hello", "好好学习,天天向上");
        response.setHeader("aaa", "bbbbbb====");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //使用request对象的getSession()获取session，如果session不存在则创建一个
        String characterEncoding = response.getCharacterEncoding();
        String contentType = response.getContentType();
        System.out.println(contentType + "======" + characterEncoding);
        //session
        HttpSession session = request.getSession();
        //将数据存储到session中
        session.setAttribute("data", "孤傲苍狼");
        //获取session的Id
        String sessionId = session.getId();
        session.getAttribute("data");
        //判断session是不是新创建的
        if (session.isNew()) {
            response.getWriter().print("session创建成功，session的id是：" + sessionId);

        } else {
            response.getWriter().print("服务器已经存在该session了，session的id是：" + sessionId);
            response.getWriter().print("服务器已经存在该session了，session的id是：" + session.getAttribute("data"));
        }
//把服务之间的调用接口返回的数据储存到redis

        List<ScwWebuisApplication.ProjectVo> data = (List<ScwWebuisApplication.ProjectVo>)redisTemplate.opsForValue().get("projectInfo");

        if(data==null) {
            AppResponse<List<ScwWebuisApplication.ProjectVo>> resp = tProjectInfoServiceFeign.all();
            log.debug("webuis----调用远程服务系统====success=");
            data = resp.getData();
            redisTemplate.opsForValue().set("projectInfo", data,1,TimeUnit.HOURS);
        }

        modle.addAttribute("projectVoList", data);//前台遍历的名称 数据放到请求域中了

        return "index";
    }

    /**

     * @Description:
     * @Param:
     * @return:
     * @Author: zy
     * @Date:
     */@RequestMapping("index1")
    public String test(Model model){
         model.addAttribute("names", Arrays.asList("zhagnsan","44","作家"));
        return "index_cop";
    }
    @RequestMapping("/doLogin")
    public String doLogin(String loginacct,String userpswd,HttpSession session) {
        AppResponse<UserRespVo> resp = memberServiceFeign.login(loginacct, userpswd);
        UserRespVo data = resp.getData();
        if(data==null) {
            return "login";
        }
        //这个是存储到session,前端取数据回显用
       session.setAttribute("loginMember", data);
        String preUrl = (String) session.getAttribute("preUrl");
        if(StringUtils.isEmpty(preUrl)){
            return "redirect:/index";
        }else {
           return "redirect:"+preUrl;
        }
    }


    @RequestMapping("/login")
    public String login() {
        return "login";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
         if (session!=null){
             session.removeAttribute("loginMember");
             session.invalidate();
         }
        return "redirect:/index";
    }
}
